home *** CD-ROM | disk | FTP | other *** search
/ Gold Medal Software 2 / Gold Medal Software Volume 2 (Gold Medal) (1994).iso / os2 / cenvi2.arj / CLIPBRD.LIB < prev    next >
Text File  |  1993-10-20  |  5KB  |  131 lines

  1. // ClibBrd.lib - Functions for reading from and writing to the Windows
  2. //               clipboard.
  3. //
  4. // This library provides the following routines:
  5. // 1) GetClipboardData(ClipboardDataFormat[,ClipboardDataSize])
  6. //     Copy data from the clipnoard, where:
  7. //       ClipboardDataFormat - one of the following data types
  8.             #define CF_TEXT         1
  9.             #define CF_BITMAP       2
  10.             #define CF_DSPTEXT      3
  11.             #define CF_DSPBITMAP    4
  12.             #define CF_METAFILE     5
  13.             #define CF_DSPMETAFILE  6
  14.             #define CF_PALETTE      9
  15. //       ClipboardDataSize - sets to the size of data returned; optional
  16. //       Returns: NULL if no data in clipboard or not of correct type, else
  17. //                returns data in buffer or BLOb of size ClipboardDataSize
  18. //
  19. // 2) PutClipboardData(ClipboardData,ClipboardDataSize,ClipboardDataFormat)
  20. //     Copy data to the clipboard, where:
  21. //       ClipboardData - buffer or BLOb of data to copy to the clipboard.
  22. //                       If this is NULL then clipboard is emptied.
  23. //       ClipboardDataSize - size of data in ClipboardData, if this is zero
  24. //                           then the clipboard is emptied
  25. //       ClipboardDataFormat - one of the data types #define'ed above
  26. //       Returns: True if data successfully copied, else False
  27.  
  28.  
  29. GetClipboardData(ClipboardDataFormat,ClipboardDataSize)
  30. {
  31.    _data = NULL;  // assume failure
  32.    if ( OpenClipboard() ) {
  33.  
  34.       #define ORD_WIN32QUERYCLIPBRDDATA  806
  35.       _CPDataPtr = PMDynamicLink("PMWIN",ORD_WIN32QUERYCLIPBRDDATA,BIT32,CDECL,
  36.                                  PMInfo().hab,ClipboardDataFormat);
  37.  
  38.       if ( NULL != _CPDataPtr ) {
  39.          // query memory for how big _CPDataPtr is
  40.          BLObPut(lenBLOb,16384,UWORD32);
  41.          #define ORD_DOS32QUERYMEM               306
  42.          if ( 0 == PMDynamicLink("DOSCALLS",ORD_DOS32QUERYMEM,BIT32,CDECL,
  43.                                  _CPDataTextPtr,lenBLOb,flags) ) {
  44.             if ( 0 != (_len = BLObGet(lenBLOb,0,UWORD32)) ) {
  45.                _data = PMpeek(_CPDataPtr,_len);
  46.                if ( 1 < va_arg() )
  47.                   ClipboardDataSize = _len;
  48.             }
  49.          }
  50.       }
  51.       CloseClipboard();
  52.    }
  53.    return(_data);
  54. }
  55.  
  56. PutClipboardData(ClipboardData,ClipboardDataSize,ClipboardDataFormat)
  57. {
  58.    bool PutClipSuccess = FALSE;  // assume failure
  59.    if ( OpenClipboard() ) {
  60.  
  61.       // empty clipboard of its current contents
  62.       #define ORD_WIN32EMPTYCLIPBRD 733
  63.       PMDynamicLink("PMWIN",ORD_WIN32EMPTYCLIPBRD,BIT32,CDECL,PMInfo().hab);
  64.  
  65.       if ( NULL == ClipboardData  ||  0 == ClipboardDataSize ) {
  66.          // no data to put, so only needed to empty clipboard
  67.          PutClipSuccess = TRUE;
  68.       } else {
  69.  
  70.          // allocate giveable shared memory, and copy this data to there
  71.          #define ORD_DOS32ALLOCSHAREDMEM  300
  72.          #define PAG_COMMIT   0x10
  73.          #define OBJ_GIVEABLE 0x200
  74.          #define PAG_READ     0x1
  75.          #define PAG_WRITE    0x2
  76.          if ( 0 == PMDynamicLink("DOSCALLS",ORD_DOS32ALLOCSHAREDMEM,BIT32,CDECL,
  77.                                  _GiveMem,NULL,ClipboardDataSize*2,
  78.                                  PAG_COMMIT|OBJ_GIVEABLE|PAG_READ|PAG_WRITE ) ) {
  79.             PMpoke(_GiveMem,ClipboardData,ClipboardDataSize);
  80.  
  81.             // finally, give this to the clipboard
  82.             #define ORD_WIN32SETCLIPBRDDATA  854
  83.             #define CFI_POINTER  0x0400
  84.             if ( PMDynamicLink("PMWIN",ORD_WIN32SETCLIPBRDDATA,BIT32,CDECL,
  85.                                PMInfo().hab,_GiveMem,ClipboardDataFormat,CFI_POINTER) ) {
  86.                PutClipSuccess = True;
  87.             } else {
  88.                // error giving data to clipboad, and so must free it here
  89.                #define ORD_DOS32FREEMEM   304
  90.                PMDynamicLink("DOSCALLS",ORD_DOS32FREEMEM,BIT32,CDECL,_GiveMem);
  91.             }
  92.          }
  93.       }
  94.       CloseClipboard();
  95.    }
  96.    return PutClipSuccess;
  97. }
  98.  
  99.  
  100. /******* ROUTINES USED BY THE ABOVE FUNCTIONS *******/
  101.  
  102. // make sure clipboard gets closed before we exit
  103.  
  104.    ClipboardIsOpen = FALSE;
  105.    atexit("CloseClipboardIfOpen");
  106.  
  107.    CloseClipboardIfOpen()
  108.    {
  109.       if ( ClipboardIsOpen )
  110.          CloseClipboard();
  111.    }
  112.  
  113. OpenClipboard() // Return TRUE if open, else FALSE if cannot open
  114. {
  115.    assert( !ClipboardIsOpen );
  116.    #define ORD_WIN32OPENCLIPBRD  793
  117.    if ( PMDynamicLink("PMWIN",ORD_WIN32OPENCLIPBRD,BIT32,CDECL,PMInfo().hab) )
  118.       ClipboardIsOpen = TRUE;
  119.    return( ClipboardIsOpen );
  120. }
  121.  
  122. CloseClipboard()  // close clipboard if we have it open
  123. {
  124.    if ( ClipboardIsOpen ) {
  125.       #define ORD_WIN32CLOSECLIPBRD 707
  126.       assert( PMDynamicLink("PMWIN",ORD_WIN32CLOSECLIPBRD,BIT32,CDECL,PMInfo().hab) );
  127.       ClipboardIsOpen = FALSE;
  128.    }
  129. }
  130.  
  131.